module.exports = function($scope, $state, $stateParams, $resource, toaster, FileUploader){

    $scope.activity = {};
    $resource('/api/as/tc/sale/findSaleForVolList', {}, {}).save({}, function (res) {
        if (res.errcode === 0){
            $scope.saleList = res.data;
            $scope.saleMap = {};
            for (let index = 0; index < $scope.saleList.length; index++) {
                const element = $scope.saleList[index];
                $scope.saleMap[element.code] = element.name;
            }
        } else {
            toaster.error({title: '', body: res.errmsg});
        }
    });
    if ($stateParams.type == '1') {
        $scope.is_edit = true;
        $scope.url = '/api/as/ggc/activity/update';
        $resource('/api/as/ggc/activity/info', {}, {}).save({id: $stateParams.id}, function (res) {
            if (res.errcode === 0){
                $scope.activity = res.data;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    } else {
        $scope.activity.sort = 1;
        $scope.url = '/api/as/ggc/activity/save';
    }

    $scope.pageInfo = {}
    function date2str(d) {
		if (d === undefined) {
			return "";
		}
		var month = (d.getMonth() + 1).toString();
		var day = d.getDate().toString();
		if (month.length == 1) month = '0' + month;
		if (day.length == 1) day = '0' + day;
		return d.getFullYear() + "-" + month + "-" + day;
	}

	$scope.dateOpen = function ($event, item) {
		$event.preventDefault();
		$event.stopPropagation();
		item.opened = true;
	};

	$scope.period_start = {
		'value': date2str(new Date()),
		// 'value': '',
		'opened': false
	}

	$scope.period_end = {
		'value': date2str(new Date()),
		// 'value': '',
		'opened': false
	}

	$scope.gogo = function(){

        $scope.activity.start_time = (typeof $scope.period_start.value === 'string' ? $scope.period_start.value : date2str($scope.period_start.value));
        $scope.activity.end_time = (typeof $scope.period_end.value === 'string' ? $scope.period_end.value : date2str($scope.period_end.value));
        $scope.activity.sale_name = $scope.saleMap[$scope.activity.sale_code];
		$resource($scope.url, {}, {}).save($scope.activity, function (res) {
            if (res.errcode === 0){
                toaster.success({title: '', body: '创建成功'});
                $state.go('app.tourActivityList')
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    };
    
    $scope.uploader = new FileUploader({
        url: 'https://dlqt.juyouhx.com/Api/Api/ObjectToOss?topdir=aa&selfdir=bb'
    });

    $scope.uploader.filters.push({
        name: 'imageFilter',
        fn: function (item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
    });

    $scope.uploader.onSuccessItem = function (fileItem, response, status, headers) {
        $scope.activity.img = response.savename;
    };
	
};